package cn.manyufun.myf.ui.comics.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import cn.manyufun.myf.R;
import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.app.AppConstant;
import cn.manyufun.myf.bean.HotArticlesBean;
import cn.manyufun.myf.bean.NewsSummary;
import cn.manyufun.myf.common.base.BaseFragment;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.baserx.RxSubscriber;
import cn.manyufun.myf.common.commonutils.APIUtils;
import cn.manyufun.myf.common.irecyclerview.universaladapter.ViewHolderHelper;
import cn.manyufun.myf.common.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import cn.manyufun.myf.ui.common.CommonListActivity;
import cn.manyufun.myf.ui.news.activity.NewsDetailActivity;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by Xiao on 2018-02-07.
 */

public class ComicsArticleFragment extends BaseFragment {
    @BindView(R.id.tv_hot)
    TextView mTvHot;
    @BindView(R.id.lv_hot)
    RecyclerView mLvHot;
    @BindView(R.id.tv_new)
    TextView mTvNew;
    @BindView(R.id.lv_new)
    RecyclerView mLvNew;
    @BindView(R.id.tv_go_all)
    TextView mTvGo;
    private long mComicsId;

    CommonRecycleViewAdapter<NewsSummary> mHotAdapter;
    CommonRecycleViewAdapter<NewsSummary> mNewAdapter;


    public static ComicsArticleFragment newInstance(Bundle args) {
        ComicsArticleFragment fragment = new ComicsArticleFragment();
        if (args != null)
            fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.comics_comment_list;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        Bundle args = getArguments();
        if (args != null)
            mComicsId = args.getLong("comicsid");
        mTvHot.setText("热门漫评");
        mTvNew.setText("最新漫评");
        mTvGo.setText("查看全部漫评>");
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        mHotAdapter = new CommonRecycleViewAdapter<NewsSummary>(getContext(), R.layout.layout_item_article) {
            @Override
            public void convert(ViewHolderHelper holder, final NewsSummary newsSummary) {
                holder.setText(R.id.tv_author_name, newsSummary.getAuthorName());
                holder.setImageRoundUrl(R.id.iv_head, newsSummary.getAuthorImageUrl());
                holder.setRating(R.id.rb_score, newsSummary.getScore());
                holder.setText(R.id.tv_title, newsSummary.getTitle());
                holder.setText(R.id.tv_content, newsSummary.getBriefInfo());
                holder.setText(R.id.tv_like, newsSummary.getLikeNum() + "");
                holder.setText(R.id.tv_comment, newsSummary.getCommentNum() + "");
                holder.setText(R.id.tv_view, newsSummary.getViewNum() + "");
                holder.setText(R.id.tv_publish_time, dateFormat.format(newsSummary.getPublishTime()));
                holder.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putLong(NewsDetailActivity.EXTRA_DATA_ARTICLE_ID, newsSummary.getReferenceId());
                        bundle.putString(NewsDetailActivity.EXTRA_DATA_ARTICLE_TITLE, newsSummary.getTitle());
                        bundle.putString(NewsDetailActivity.EXTRA_DATA_ARTICLE_IMG, newsSummary.getCoverImageUrl());
                        NewsDetailActivity.startAction(mContext, bundle);
                    }
                });
            }
        };
        mNewAdapter = new CommonRecycleViewAdapter<NewsSummary>(getContext(), R.layout.layout_item_article) {
            @Override
            public void convert(ViewHolderHelper holder, final NewsSummary newsSummary) {
                holder.setText(R.id.tv_author_name, newsSummary.getAuthorName());
                holder.setImageRoundUrl(R.id.iv_head, newsSummary.getAuthorImageUrl());
                holder.setRating(R.id.rb_score, newsSummary.getScore());
                holder.setText(R.id.tv_title, newsSummary.getTitle());
                holder.setText(R.id.tv_content, newsSummary.getBriefInfo());
                holder.setText(R.id.tv_like, newsSummary.getLikeNum() + "");
                holder.setText(R.id.tv_comment, newsSummary.getCommentNum() + "");
                holder.setText(R.id.tv_view, newsSummary.getViewNum() + "");
                holder.setText(R.id.tv_publish_time, dateFormat.format(newsSummary.getPublishTime()));
                holder.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putLong(NewsDetailActivity.EXTRA_DATA_ARTICLE_ID, newsSummary.getReferenceId());
                        bundle.putString(NewsDetailActivity.EXTRA_DATA_ARTICLE_TITLE, newsSummary.getTitle());
                        bundle.putString(NewsDetailActivity.EXTRA_DATA_ARTICLE_IMG, newsSummary.getCoverImageUrl());
                        NewsDetailActivity.startAction(mContext, bundle);
                    }
                });
            }
        };


        LinearLayoutManager layoutHotManager = new LinearLayoutManager(getContext());
        layoutHotManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutHotManager.setAutoMeasureEnabled(true);
        LinearLayoutManager layoutNewManager = new LinearLayoutManager(getContext());
        layoutNewManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutNewManager.setAutoMeasureEnabled(true);
        // 设置布局管理器
        mLvHot.setLayoutManager(layoutHotManager);
        mLvNew.setLayoutManager(layoutNewManager);
        mLvHot.setAdapter(mHotAdapter);
        mLvNew.setAdapter(mNewAdapter);

        Api.getDefault(HostType.manyufun).getHotArticles(getHotRequestBody())
                .compose(RxSchedulers.<HotArticlesBean>io_main())
                .subscribe(new RxSubscriber<HotArticlesBean>(getContext(), false) {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    protected void _onNext(HotArticlesBean posterBean) {
                        mHotAdapter.replaceAll(posterBean.getHotArticleList());
                        mNewAdapter.replaceAll(posterBean.getNewArticleList());

                        if (posterBean.getHotArticleList().size() == 0)
                            mTvHot.setVisibility(View.GONE);
                        else
                            mTvHot.setVisibility(View.VISIBLE);
                        if (posterBean.getNewArticleList().size() == 0)
                            mTvNew.setVisibility(View.GONE);
                        else
                            mTvNew.setVisibility(View.VISIBLE);

                        if (posterBean.getHotArticleList().size() == 0 && posterBean.getNewArticleList().size() == 0)
                            mTvGo.setText("还没有漫评呢。");
                        else {
                            mTvGo.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Bundle bundle = new Bundle();
                                    bundle.putInt(CommonListActivity.EXTRA_DATA_TYPE, AppConstant.TYPE_ARTICLE);
                                    bundle.putLong(CommonListActivity.EXTRA_DATA_OTHER, mComicsId);
                                    CommonListActivity.startAction(getContext(), bundle);
                                }
                            });
                        }
                    }

                    @Override
                    protected void _onError(String message) {

                    }
                });

    }

    private RequestBody getHotRequestBody() {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ComicId", mComicsId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return RequestBody.create(MediaType.parse("application/json"), root.toString());
    }
}
